<?php

namespace src\common;

class Request
{

    private static $params = [];
    private static $header = [];

    public static function init()
    {

        self::$params = array_merge(self::$params, self::getJson());

        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                // 将 HTTP_ 替换为 - 并将所有字符转换为小写
                self::$header[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
    }

    private static function getJson(): ?array
    {
        if (
            isset($_SERVER['CONTENT_TYPE']) 
            && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false
            && $input = file_get_contents('php://input')
        ) {
            return json_decode($input, true);
        }
        return [];
    }

    public static function params(): array
    {
        return self::$params;
    }

    public static function header(): array
    {
        return self::$header;
    }

}
